const {ipcMain} = require('electron')
const {send: sendMainWindow} = require('./window/main')
const {createControlWindow, send: sendControlWindow} = require('./window/control')
const signal = require('./signal')

module.exports = function () {
    ipcMain.handle('login', async () => {
        // return Math.floor(Math.random() * (999999))
        let {code} = await signal.invoke('login', null, 'logined')
        return code
    })

    ipcMain.on('control', async (e, remote) => {
        //这里跟服务器交互
        signal.send('control', {remote})
    })

    ipcMain.on('forward', (e, event, data) => {
        signal.send('forward', {event, data})
    })


    signal.on('controlled', data => {
        sendMainWindow('control-status-change', data.remote, 1)
        createControlWindow()
    })
    signal.on('be-controlled', data => {
        sendMainWindow('control-status-change', data.remote, 2)
    })
    signal.on('offer', data => {
        sendMainWindow('offer', data)
    })
    signal.on('answer', data => {
        sendControlWindow('answer', data)
    })
    signal.on('puppet-candidate', data => {
        sendControlWindow('candidate', data)
    })
    signal.on('control-candidate', data => {
        sendMainWindow('candidate', data)
    })
}
